package org.vectrics.recruiter.struts.tag;

/**
 * @author Mike Moore
 */
import org.vectrics.company.Company;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.recruiter.RecruitServiceLocator;
import java.util.*;
import org.apache.log4j.*;
import org.vectrics.DataException;
import org.vectrics.SystemException;
import org.vectrics.recruiter.job.Job;
import org.vectrics.user.Person;


/**
 * A HTML combo box to allow selection of a recruiter.
 * @author Mike Moore
 *
 */
public class RecruiterCombo extends PeopleCombo {

	private static final long serialVersionUID = 9129124386597949310L;
	
	static private Category log = Category.getInstance(RecruiterCombo.class);
    private String addNoPersonOption = null;
    private Long jobId = null;
    private Company company = null;
    
    
    public RecruiterCombo(Company company, Job job) {
    	super();
    	if (job != null)
    		this.jobId = job.getId();
   		this.company = company;
   		if (this.company == null)
   			throw new SystemException("Company passed was null");
   		this.addPeople();
    }
    

	public Company getCompany() {
		return company;
	}


	public Long getJobId() {
		return jobId;
	}


	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}


	protected boolean getAddNoPersonOption() {
		boolean result = false;
		if (addNoPersonOption.equals("true")) {
			result = true;
		} else if (addNoPersonOption.equals("false")) {
			result = false;
		} else {
			throw new SystemException("Illegal value for 'addNotPersonOption' needs to be 'true' or 'fale' - value is " + addNoPersonOption);
		}
		return(result);
	}

	public void setAddNoPersonOption(String value) {
		this.addNoPersonOption = value;
	}
	
	protected Collection findPersons() {
		log.debug("findPersons() - called");
		if (this.company == null)
			throw new SystemException("Company cannot be null");
		Collection persons = RecruitServiceLocator.getRecruitCompanyService().findRecruiters(company);
		if (persons.size() == 0)
			persons.add(ThreadProperties.getPerson());
		return(persons);
	}
	
	protected Long getSelectedPersonId() {
		if (getProperty() == null) {
			log.error("getSelectedUserId() - param called 'property' is NULL and should have a value");
			throw new DataException("getSelectedUserId() - param called 'property' is NULL and should have a value");
		}
			
		log.debug("getSelectedUserId()");
		Job job = null;
		if (jobId != null) {
			job = RecruitServiceLocator.getJobProvider().findJob(new Long(jobId));
		}

		if (job == null) {
			log.debug("getSelectedUserId() - Could not find job with id");
		}
		else {
			log.debug("getSelectedUserId() - Found job with id");
		}

		log.debug("getSelectedUserId() - finding employee");
		Person employee = null;
		if (job != null) {
			if (getProperty().toLowerCase().indexOf("prim") >= 0) {
				log.info("getSelectedUserId() - finding primary recruiter");
				employee = job.getPrimaryRecruiter();
			}
			else {
				log.info("getSelectedUserId() - finding secondary recruiter");
				employee = job.getSecondaryRecruiter();
			}
		}
		
		log.debug("getSelectedUserId() - finding employee id");
		Long employeeUserId = null;
		if (employee != null) {
			log.debug("employee found, recording id of employee");
			employeeUserId = employee.getId();
		}
		else {
			log.debug("employee not found");
		}
		return(employeeUserId);		
	}

	protected boolean showBlank() {
		return true;
	}
	
}
